package com.muqi.app.pj.shops;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.LogUtil;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

import com.muqi.app.user.widget.MyGridView;
import com.muqi.app.widget.pulltorefresh2.XScrollView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class FictitiousActivity extends BaseFragmentActivity implements
		XScrollView.IXScrollViewListener, OnItemClickListener, OnClickListener {
	private XScrollView mScrollView;

	private int page = 1;

	private MyGridView gv_xuni;
	private FictitiousAdapter adapter;
	private List<FictitiousShopInfo> prizeInfos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fictitious);
	}

	@SuppressLint("InflateParams")
	@Override
	protected void onInit() {
		mScrollView = (XScrollView) findViewById(R.id.xMyScrollView);
		mScrollView.setPullRefreshEnable(true);
		mScrollView.setPullLoadEnable(false);
		mScrollView.setAutoLoadEnable(true);
		mScrollView.setIXScrollViewListener(this);
		mScrollView.setRefreshTime("刚刚");
		View content = LayoutInflater.from(this).inflate(R.layout.activity_fic_content, null);
		mScrollView.setView(content);
		gv_xuni = (MyGridView)content.findViewById(R.id.shop_gv_xuni);
		gv_xuni.setOnItemClickListener(this);
		prizeInfos=new ArrayList<FictitiousShopInfo>();
		adapter=new FictitiousAdapter(prizeInfos, mContext);
		gv_xuni.setAdapter(adapter);

	}

	@Override
	protected void onData() {
	
		loadingFictitious();
	}

	/**
	 * 加载虚拟商品
	 */
	private void loadingFictitious() {
		HashMap<String, Object> sendMap = new HashMap<String, Object>();
		sendMap.put("token", mSpUtil.getToken());
		sendMap.put("class_manager_id", mSpUtil.getChildClassId());
		String requestUrl = ParamsUtils.buildParams(
				NetWorkApi.XUNI_GOODS_LIST_API, sendMap);
		MyAsyncHttp.get(mContext, requestUrl, new AsyncCallBackListener() {

			@Override
			public void onAsyncCallBack(RESULTCODE resuleCode, String request,
					String result) {
				mScrollView.stopLoadMore();
				mScrollView.stopRefresh();
				mScrollView.setPullLoadEnable(false);
				if (resuleCode == RESULTCODE.SUCCESS) {

					List<FictitiousShopInfo> temp= ResponseUtils.getFictitiousShopInfo(mContext, result);
					if (prizeInfos != null) {
						prizeInfos=temp;
						adapter = new FictitiousAdapter(prizeInfos,
								mContext);
						gv_xuni.setAdapter(adapter);

					}

				}

			}
		});

	}



	public void onBack(View v) {
		this.finish();
	}


	

	
	@Override
	public void onRefresh() {
		LogUtil.e("刷新");
		page = 1;
		loadingFictitious();
	}

	@Override
	public void onLoadMore() {
		LogUtil.e("加载更多");
		loadingFictitious();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		FictitiousShopInfo bean = prizeInfos.get(arg2);
		if (bean != null) {
			Intent intent = new Intent(mContext, FictiousShopDetialActivity.class);
			intent.putExtra("shop_id", bean.id);
			startActivity(intent);
		}

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		default:
			break;
		}

	}

}
